<template>
  <view>
 <view class="search-box">
   <uni-search-bar class="uni-mt-10" @input="input" radius="50" placeholder="请输入搜索内容" clearButton="auto" cancelButton="none"  />
   </view>
   <!-- 搜索建议 -->
   <view class="sugg-list" v-if="searchlist.length!==0">
     <view class="sugg-item" v-for="(item,index) in searchlist" :key="index" @click="gotDetail(item)">
       <view class="goods-name">
         {{item.goods_name}}
       </view>
       <uni-icons  type="arrowright" size="16"></uni-icons>
     </view>
     
   </view>
   <!-- 搜索历史 -->
   <view class="history-box" v-else>
     <view class="history-titile">
       <text>搜索历史</text>
       <uni-icons type="trash" @click="del" size="22"></uni-icons>
     </view>
     <!-- 列表区域 -->
     <view class="history-list" >
       <uni-tag custom-style="background-color: #e5e5e5; border:none;color:#000;font-size:16px;padding:9px 15px"   :text="item" v-for="(item,i) in histories" :key="i" @click="gogoodslist(item)"></uni-tag>
       
     </view>
   </view>

  </view>
</template>

<script>
  export default {
    data() {
      return {
        val:'',
        timer:undefined,
        searchlist:[],
        historylist:['a','app','cx']
      };
    },
    onLoad() {
      this.historylist=JSON.parse(uni.getStorageSync('uni-history'))
    },
    computed:{
      histories(){
      return [...this.historylist].reverse()
      }
    },
    methods:{
      //输入内容调接口
      //如果频繁掉的话就把上次的定时器清掉，所以就执行一次实现防抖
      input(e){
      clearTimeout(this.timer)
     this.timer= setTimeout(()=>{
        this.val=e.trim()
       this.getsearchlist()
      },1000)
      },
      //获取数据的接口
     async getsearchlist(){
        if(this.val.length===0){
          this.searchlist=[]
          return
        }
      let {data:res}=await  uni.$http.get('/api/public/v1/goods/qsearch', { query: this.val })

      if (res.meta.status !== 200) return uni.$showMsg()
        
      if(res.message.length===0) {
        uni.$showMsg('没有找到相关内容')
        return
      }
        this.searchlist = res.message
        this.saveSearchHistory()
      },
      gotDetail(item){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
        })
      },
      saveSearchHistory(){
        const set=new Set(this.historylist)
        set.delete(this.val)
        set.add(this.val)
        this.historylist=Array.from(set)
        uni.setStorageSync('uni-history',JSON.stringify(this.historylist))
      },
      del(){
      
        this.historylist=[],
        uni.setStorageSync('uni-history','[]')
      },
      gogoodslist(item){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query='+item
        })
      }
    }
  }
</script>

<style lang="scss">
.search-box{
  position: sticky;
  z-index: 999;
  top:0
}
.sugg-list{
  padding:0 5px;
  .sugg-item{
      display:flex;
      justify-content: space-between;
      border-bottom: 1px solid #efefef;
    .goods-name{
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      font-size: 12px;
      padding: 13px 0;
    }
  }
}
.history-box{
  padding: 0 5px;
  .history-titile{
    display: flex;
    justify-content: space-between;
    height: 40px;
    align-items: center;
    font-size: 16px;
    border-bottom: 1px solid #efefef;
  }
  .history-list{
    display: flex;
    flex-wrap: wrap; margin-top:15px ;
    uni-tag{
     
      margin-right: 5px;
    }
  }
  
  
}
</style>
